/*
 * Copyright (C) 2012 yueyueniao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mobilezu.mobile.android.mobile4s.view.slidingmenu.fragment;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.mobilezu.mobile.android.mobile4s.R;
import com.mobilezu.mobile.android.mobile4s.activity.MainActivity;
import com.mobilezu.mobile.android.mobile4s.util.CommonHelper;

/**
 * Main content fragment
 * 
 * @author leo.wang
 * @create 2013-5-28
 * @version 1.0.0
 */
public class MainContentFragment extends Fragment {
	private Button btnMenu;
	private Button btnEdit;
	private ViewFragmentPagerAdapter mAdapter;
	private ViewPager mPager;
	private ArrayList<Fragment> pagerItemList = new ArrayList<Fragment>();
	private View mView;
	private PageChangeListener mPageChangeListener;
	private MainListFragment mMainListFragment;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.main_pager_layout, null);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initViews();
		initListener();
		initData(getArguments());
	}

	/**
	 * Initialize
	 */
	private void initViews() {
		try {
			btnMenu = (Button) mView.findViewById(R.main_pager_layout.btn_menu);
			btnEdit = (Button) mView.findViewById(R.main_pager_layout.btn_edit);
			mPager = (ViewPager) mView.findViewById(R.main_pager_layout.pager);
		} catch (Exception e) {
			CommonHelper.log(this.getClass().getName(),
					"initViews:" + e.getMessage());
		}
	}

	/**
	 * Initialize listview data
	 * 
	 * @param data
	 */
	public void initData(Bundle data) {
		try {
			mMainListFragment = new MainListFragment();
			pagerItemList.add(mMainListFragment);
			mAdapter = new ViewFragmentPagerAdapter(getFragmentManager());
			mPager.setAdapter(mAdapter);

			// Initialize listview data
			mMainListFragment.setArguments(data);
		} catch (Exception e) {
			CommonHelper.log(this.getClass().getName(),
					"initData:" + e.getMessage());
		}
	}

	/**
	 * Initialize listener
	 */
	private void initListener() {

		// Set on show left panel click listener
		btnMenu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				((MainActivity) getActivity()).showLeft();
			}
		});

		// Set on refresh click listener
		btnEdit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});

		// Set onpage listener
		mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {

				if (mPageChangeListener != null)
					mPageChangeListener.onPageSelected(position);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int position) {

			}
		});
	}

	/**
	 * Page Adapter
	 * 
	 * @create 2013-5-29
	 * @version 1.0.0
	 */
	public class ViewFragmentPagerAdapter extends FragmentPagerAdapter {
		public ViewFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return pagerItemList.size();
		}

		@Override
		public Fragment getItem(int position) {
			Fragment fragment = null;
			if (position < pagerItemList.size())
				fragment = pagerItemList.get(position);
			else
				fragment = pagerItemList.get(0);
			return fragment;
		}
	}

	public void setOnPageChangeListener(PageChangeListener onPageChangeListener) {
		this.mPageChangeListener = onPageChangeListener;
	}

	public interface PageChangeListener {
		public void onPageSelected(int position);
	}

}
